<template>
  <div class="departmentManage">
    <CommonBtn label-name="部门信息列表" :buttons="buttons" @onButtonClick="onButtonClick">
    </CommonBtn>
    <CommonList ref="list" :buttons="buttons"
                :columns="columns" :listUrl="listUrl" @tableSelect="tableSelect"
                row-key="organizationId"></CommonList>
    <edit-pop-up  v-if="add" :out-btn="button"
                  :props="add" @closeDialog="button=null" :original-data="row"
                  @refresh="refresh" :submit-url="$api.API_DEPARTMENT_ADD" :edit-url="$api.API_DEPARTMENT_UPDATE"></edit-pop-up>

    <EnsurePopUp :out-btn="button" @close="editBack" @refresh="refresh" :original-data="row"
                 :delete-url="$api.API_DEPARTMENT_DELETE"></EnsurePopUp>
  </div>
</template>

<script>
    import CommonList from "../../common/CommonList";
    import CommonBtn from "../../common/CommonBtn";
    import CommonEdit from "../../common/CommonEdit";
    import EditPopUp from "../../common/EditPopUp";
    import EnsurePopUp from "../../common/EnsurePopUp";
    export default {
        name:'departmentManage',
        data(){
            return{
                buttons:[
                    this.$config.BTNS.ADD,
                    this.$config.BTNS.EDIT,
                    this.$config.BTNS.DELETE,
                ],
                columns:[
                    {
                        prop:'organizationName',
                        label:'组织机构'
                    },
                    {
                        prop:'organizationCode',
                        label:'部门编号'
                    },
                    {
                        prop:'organizationManager',
                        label:'主负责人'
                    },
                    {
                        prop:'organizationInnerPhone',
                        label:'外线电话'
                    },
                    {
                        prop:'organizationOuterPhone',
                        label:'内线电话'
                    },
                    {
                        prop:'',
                        label:'传真号码'
                    },
                    {
                        prop:'organizationZipcode',
                        label:'邮政区号'
                    },
                    {
                        prop:'sortCode',
                        label:'显示顺序'
                    },
                    {
                        prop:'organizationAddress',
                        label:'所在地址'
                    }
                ],
                add:{
                    title:'部门信息',
                    addItems:[
                        {name:'部门编号',prop:'organizationCode',type:'text',required:true},
                        {name:'机构名称',prop:'organizationName',type:'text',required:true},
                        {name:'主负责人',prop:'organizationManager',type:'text',required:true},
                        {name:'副负责人',prop:'organizationAssistantManager',type:'text'},
                        {name:'外线电话',prop:'organizationOuterPhone',type:'text'},
                        {name:'内线电话',prop:'organizationInnerPhone',type:'text'},
                        {name:'传真号码',prop:'organizationFax',type:'text'},
                        {name:'邮政区号',prop:'organizationZipcode',type:'text'},
                        {name:'节点位置',prop:'parentId',type:'select',options:[],required:true},
                        {name:'显示顺序',prop:'sortCode',type:'text',required:true},
                        {name:'所在地址',prop:'organizationAddress',type:'text'}
                    ],
                    type:'dialog'
                },
                listUrl:this.$api.API_DEPARTMENT_LIST,
                button:null,
                //新增的节点位置有下拉框
                row:{}
            }
        },
        methods:{
            onButtonClick(item) {
                if (item.event == 'edit()') {
                    if (!this.row || JSON.stringify(this.row) == '{}') {
                        this.$message.error("请选择一行数据")
                        return
                    }
                }
                if (item.event == 'add()') {
                    this.row = {}
                }

                this.button = item
            },
            editBack(item) {
                this.button = item
            },
            tableSelect(row) {
                this.row = row
            },
            initData() {
                this.$http.post(this.$api.API_DEPARTMENT_INIT).then(resp=>{
                    let nodeList = [{label:'部门信息 - 父节',value:"0"}]
                    for (let i=0;i<resp.length;i++) {
                        let node = {value:resp[i].organizationId,label:resp[i].organizationName}
                        nodeList.push(node)
                        this.add.addItems[8].options = nodeList
                    }
                },msg=>{})
            },
            refresh() {
                this.$refs.list.search()
            }
        },
        created() {
            this.initData()
        },
        components:{CommonList,CommonBtn,CommonEdit,EditPopUp,EnsurePopUp}
    }
</script>
